package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.ExtendQuotationInfoDto;
import com.hxgz.steel.service.NewOrderService;
import com.hxgz.steel.service.QuotationManageService;
import com.hxgz.steel.dto.QuotationInfoDto;
import com.hxgz.steel.dto.QuotationManageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * 报价管理
 */
@RestController
@RequestMapping("quotationManage")
@Api(tags = "QuotationManageController", description = "前台-卖家中心-报价管理")
public class QuotationManageController {

    @Autowired
    private QuotationManageService quotationManageService;
    @Autowired
    private NewOrderService newOrderService;

    /**
     * 报价列表
     *
     * @return
     */
    @RequestMapping(value = "listQuotations.do", method = RequestMethod.POST)
    @ApiOperation("报价管理-报价列表")
    public ServerResponse<PageInfo<List<QuotationManageDto>>> listQuotations(@RequestParam(required = false) @ApiParam("起始提交时间") String updateTimeStart, @RequestParam(required = false) @ApiParam("截止提交时间") String updateTimeEnd,
                                                                             @RequestParam(required = false) @ApiParam("状态 0未报价  1已报价  2已拒绝 不填-全部") Integer status, Page page, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        QuotationManageDto dto = new QuotationManageDto();
        dto.setStatus(status);
        dto.setUpdateTimeStart(updateTimeStart);
        dto.setUpdateTimeEnd(updateTimeEnd);
        return quotationManageService.listQuotations(dto, user.getId(), page);
    }

    /**
     * 报价详情
     *
     * @param id 实单求购id
     * @return
     */
    @RequestMapping(value = "getQuotationInfo.do", method = RequestMethod.POST)
    @ApiOperation("报价管理-报价详情")
    public ServerResponse getQuotationInfo(Long id, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return quotationManageService.getQuotationInfo(id, user.getId());
    }

    /**
     * 下载求购文件
     *
     * @param id 实单求购id
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "downloadQuotation.do", method = RequestMethod.GET)
    @ApiOperation("报价管理-下载求购文件")
    public void downloadQuotation(Long id, HttpServletResponse response, HttpServletRequest request) {
        User user = Const.checkUserLogin(request);
        if (user == null) {
            return;
        }
        quotationManageService.downloadQuotation(id, user.getId(), response, request);
    }


    /**
     * 上传报价文件
     *
     * @return
     */
    @RequestMapping(value = "importQuotation.do", method = RequestMethod.POST)
    @ApiOperation("报价管理-上传报价文件")
    public ServerResponse importQuotation(MultipartFile multipartFile, HttpServletRequest request) {
        return quotationManageService.importQuotation(multipartFile, request);
    }

    /**
     * 拒绝报价
     *
     * @return
     */
    @RequestMapping(value = "refuseQuotation.do", method = RequestMethod.POST)
    @ApiOperation("报价管理-拒绝报价")
    public ServerResponse refuseQuotation(Long id, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return quotationManageService.refuseQuotation(id, user.getId());
    }

    /**
     * 提交报价
     *
     * @param vo           总报价，求购id
     * @param -fileAddress 上传的文件地址
     * @return
     */
    @RequestMapping(value = "submitQuotation.do", method = RequestMethod.POST)
//    @PostMapping(value = "submitQuotation.do",headers = "content-type=multipart/form-data")
    @ApiOperation("报价管理-提交报价")
    public ServerResponse submitQuotation(QuotationInfoDto vo, @RequestParam(required = false) String fileAddress, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if(vo.getTransactionPeriod()!=null){
            String s = vo.getTransactionPeriod().toString();
            boolean matches = s.matches("^[1-9]\\d*$");
            if(!matches){
                return ServerResponse.createByErrorMessage("交易周期请输入正整数!");
            }
        }

        return quotationManageService.submitQuotation(vo, fileAddress, user.getId());
    }

    @RequestMapping(value = "wxSubmitQuotation.do", method = RequestMethod.POST)
    @ApiOperation("报价管理-微信提交报价")
    public ServerResponse wxSubmitQuotation(@RequestBody ExtendQuotationInfoDto vo, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return quotationManageService.submitQuotation(vo, vo.getFileAddress(), user.getId());
    }

    @RequestMapping(value = "getAskFileUrlByMiniPrograms.do", method = RequestMethod.POST)
    @ApiOperation("小程序求购文件下载")
    public ServerResponse getAskFileUrlByMiniPrograms(Long id, HttpServletResponse response, HttpServletRequest request) {
        if (id == null) {
            return ServerResponse.createByErrorMessage("缺少必要参数");
        }
        return newOrderService.getAskFileUrlByMiniPrograms(id,response,request);
    }

    @RequestMapping(value = "getQuotationFileUrlByMiniPrograms.do", method = RequestMethod.POST)
    @ApiOperation("小程序报价文件下载")
    public ServerResponse getQuotationFileUrlByMiniPrograms(Long id, HttpServletResponse response, HttpServletRequest request) {
        if (id == null) {
            return ServerResponse.createByErrorMessage("缺少必要参数");
        }
        return newOrderService.getQuotationFileUrlByMiniPrograms(id,response,request);
    }
}
